package com.jiashihui.web.config;

import com.jiashihui.web.interceptor.FangshuaInterceptor;
import com.jiashihui.web.interceptor.Interceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * @author jiashihui
 * @describe: 拦截器适配
 * @date 2019/01/18
 */
@Configuration
public class InterceptorConfigurer extends WebMvcConfigurationSupport {

    @Bean
    public HandlerInterceptor getFangshuaInterceptor() {
        return new FangshuaInterceptor();
    }

    @Bean
    public HandlerInterceptor getMyInterceptor() {
        return new Interceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(getMyInterceptor()).addPathPatterns("/home*");
        registry.addInterceptor(getFangshuaInterceptor());
        super.addInterceptors(registry);
    }

    /**
     * 解决 http://127.0.0.1:8080/swagger-ui.html 访问 404
     * EnableWebMvcConfiguration被覆盖
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        super.addResourceHandlers(registry);
    }
}
